﻿using System;
using Grundy.Common;
using Grundy.Message;

namespace Grundy.Entity
{
    public class LicenseEntitlementValidity
    {
        private DateTime? _validFrom;
        private DateTime? _validTo;

        private LicenseEntitlementValidity()
        {}

        public LicenseEntitlementValidity(CustomerLicenseEntitlement licenseEntitlement)
        {
            _validFrom = licenseEntitlement.ValidFrom;
            _validTo = licenseEntitlement.ValidTo;
        }

        public LicenseEntitlementValidity(LicenseEntitlement licenseEntitlement)
        {
            _validFrom = licenseEntitlement.ValidFrom;
            _validTo = licenseEntitlement.ValidTo;
        }

        public static LicenseEntitlementValidity Null { get { return new LicenseEntitlementValidity(); } }

        public bool IsValid()
        {
            return (!_validFrom.HasValue && !_validTo.HasValue)
                   || (_validFrom.GetValueOrDefault().Date <= Clock.Instance.Utc.Date && (!_validTo.HasValue || _validTo.Value.Date >= Clock.Instance.Utc.Date));
                   
        }

        public override string ToString()
        {
            return 
                IsValid()
                ? "The license is valid from '{0:dd/MMMM/yyyy}' to '{1:dd/MMMM/yyyy}'.".FormatWith(_validFrom, _validTo)
                : "The license has either not begun yet or it has expired. The license is valid from '{0:dd/MMMM/yyyy}' to '{1:dd/MMMM/yyyy}'.".FormatWith(_validFrom, _validTo);
        }
    }
}